package com.ebt.m.commons.model.http;


import android.text.TextUtils;

import com.ebt.m.commons.BaseApplication;
import com.ebt.m.commons.model.data.Constants;
import com.ebt.m.commons.utils.AdviceUtil;
import com.ebt.m.commons.utils.MD5Util;
import com.ebt.m.commons.utils.PrefKit;
import com.ebt.m.commons.utils.Logger;

import org.json.JSONObject;

import java.io.IOException;

import okhttp3.HttpUrl;
import okhttp3.Interceptor;
import okhttp3.Request;
import okhttp3.Response;

/**
 * okhttp头部验证
 */
public class AuthorizationInterceptor implements Interceptor {

    @Override
    public Response intercept(Chain chain) throws IOException {
        Request oldRequest = chain.request();
        Request.Builder builder = oldRequest.newBuilder();
        builder.addHeader("EBT-Authorization", getAuthorization());
        String token = BaseApplication.getBaseApplication().getAuthorization();
        if (!TextUtils.isEmpty(token))
            builder.addHeader("Token", token);
        builder.addHeader("User-Agent", getUserAgent());
        builder.url(addParam(oldRequest));
        Request request = builder.build();
        return chain.proceed(request);
    }

    private String getUserAgent() {
        return Constants.SIGN_APP_NAME + "/V" + AdviceUtil.getApkVersionName() + "/android";
    }

    public static String getAuthorization() {
        try {
            String deviceId = PrefKit.getString(BaseApplication.getBaseApplication(), Constants.DEVICEID_KEY);
            String requestKey = MD5Util.encodeByMD5(deviceId + Constants.ORIGIN_KEY);
            long signTime = BaseApplication.getBaseApplication().currentTime();
            String sign = MD5Util.encodeByMD5(deviceId + signTime + requestKey);
            JSONObject jsonObject = new JSONObject();
            jsonObject.put("deviceId", deviceId);
            jsonObject.put("signTimestamp", signTime);
            jsonObject.put("signVersion", Constants.SIGN_VERSION);
            jsonObject.put("sign", sign);
            Logger.d("authorization", jsonObject.toString());
            return jsonObject.toString();
        } catch (Exception e) {
            Logger.e(e);
            return null;
        }
    }

    private HttpUrl addParam(Request oldRequest) {
        HttpUrl.Builder builder = oldRequest.url()
                .newBuilder()
                .setEncodedQueryParameter("deviceId", PrefKit.getString(BaseApplication.getBaseApplication(), Constants.DEVICEID_KEY))
                .setEncodedQueryParameter("app_id", "zyj")
                .setEncodedQueryParameter("app_version", AdviceUtil.getApkVersionCode())
                .setEncodedQueryParameter("app_version_name", AdviceUtil.getApkVersionName())
                .setEncodedQueryParameter("app_source", AdviceUtil.getMetadata(BaseApplication.getBaseApplication(), "TD_CHANNEL_ID"))
                .setEncodedQueryParameter("app_os", AdviceUtil.getSystemVersion());

        return builder.build();
    }
}
